############################################################################
#
# logging.pm
#
# General Purpose logging module
#
# Copyright (C) 2006 The University of Chicago
#
# Darren Young <darren.young@gsb.uchicago.edu>
#
# $Id: logging.pm,v 1.5 2006/07/12 18:37:59 dyoung2 Exp $
#
############################################################################
#
# NOTICE
# ------
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
# USA.
#
############################################################################
#
# CHANGELOG
# ---------
# 
# $Log: logging.pm,v $
# Revision 1.5  2006/07/12 18:37:59  dyoung2
#   * Added standard headers
#
# Revision 1.4  2006/07/10 02:35:37  dyoung2
#   * updated from idstools module
#
# Revision 1.2  2006/07/06 06:25:41  root
#   * Added file handle option to logmsg and debug
#
# Revision 1.1  2006/07/06 03:22:12  root
#   * Initial version.
#
############################################################################
#
# NOTES:
# ------
#
############################################################################

my  $cvsid   = '$Id: logging.pm,v 1.5 2006/07/12 18:37:59 dyoung2 Exp $';
my  @cvsinfo = split( ' ', $cvsid );
our $NAME    = File::Basename::basename($0);
our $VERSION = $cvsinfo[2];


# ------------------------------------------------------------------------------
#                             B E G I N
# ------------------------------------------------------------------------------
BEGIN {

    # Pragmas
    use strict;

    # "Standard" modules we use
    use FindBin;
    use Config::Simple;
    use POSIX qw(strftime);
    use lib "$FindBin::Bin/../lib";

    # Standard account maint modules to use
    use errcodes;
    use utils;
    
    # Modules that this module uses
    use Sys::Hostname;
    use Carp;

}


# ------------------------------------------------------------------------------ 
#                             V A R I A B L E S
# ------------------------------------------------------------------------------ 
our $LOGFILE    = "$FindBin::Bin/../log/acctmgr.log" unless($LOGFILE); 
our $CONFIGFILE = "$FindBin::Bin/../etc/acctmgr.cfg" unless($CONFIGFILE); 


# ------------------------------------------------------------------------------
#                             C O N F I G   F I L E
# ------------------------------------------------------------------------------
our $CFG = new Config::Simple( filename => $CONFIGFILE ) or die "$!\n" unless($CFG);


# ##########################################################################
# NAME        : logmsg
# DESCRIPTION : Print a formatted log message to a file handle
# ARGUMENTS   : OPTIONAL: scalar(fh)
#             : REQUIRED: scalar(message)
# RETURN      : True
# NOTES       : None
# ##########################################################################
sub logmsg {

    my ($package, $filename, $line, $subr) = caller();
    my $message;
    my $fh;

    # 1 arg form vs 2 arg form
    if ( $#_ == 0 ) {       # got 1 arg, assume dest is STDOUT
        $fh = STDOUT;
        $message = $_[0];
    } elsif ( $#_ == 1 ) {  # got 2, they want the message somewhere else
        $fh = $_[0];
        $message = $_[1];
    }

    my $n = strftime "%Y-%m-%d %H:%M:%S", localtime(time());
    print $fh "[$n] $package(): $message\n";

    return(1);
}


# ##########################################################################
# NAME        : debug
# DESCRIPTION : Print a debug formatted log message to a file handle
# ARGUMENTS   : OPTIONAL: scalar(fh)
#             : REQUIRED: scalar(message)
# RETURN      : True
# NOTES       : Set a package level variable DEBUG to enable/disable these
# ##########################################################################
sub debug { 

    my $message;
    my $fh;

    # 1 arg form vs 2 arg form
    if ( $#_ == 0 ) {       # got 1 arg, assume dest is STDOUT
        $fh = STDOUT;
        $message = $_[0];
    } elsif ( $#_ == 1 ) {  # got 2, they want the message somewhere else
        $fh = $_[0];
        $message = $_[1];
    }

    if ( $main::DEBUG ) {
        logmsg($fh, "DEBUG: $message");
        return(1); 
    } else {
        return(1);
    }
} 


# return true
1;
